package com.yanqu.road.dao.impl.activity.jiuzhou.guess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.guess.CrossJiuZhouGuessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JiuZhouRoundGuessDataDaoImpl extends TempDataObjectDao<CrossJiuZhouGuessData> {
    public static String getCrossJiuZhouGuessDataSql = "select * from `t_c_jiu_zhou_round_guess_data` where `activityId`=? and `roundId`=?";
    public CrossJiuZhouGuessData getCrossJiuZhouGuessData(int activityId,int roundId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouGuessDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, roundId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    public static String getCrossJiuZhouGuessDataMapSql = "select * from `t_c_jiu_zhou_round_guess_data` where `activityId`=?";
    public Map<Integer,CrossJiuZhouGuessData> getCrossJiuZhouGuessMapData(int activityId) {
        Map<Integer,CrossJiuZhouGuessData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouGuessDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJiuZhouGuessData crossJiuZhouGuessData = parse(rs);
                map.put(crossJiuZhouGuessData.getRoundId(),crossJiuZhouGuessData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossJiuZhouGuessDataSql = "update `t_c_jiu_zhou_round_guess_data` set guessRoomIdMap=?,allUserGuessNum=?,guessResultMap=?,roomWinnerMap=? where activityId=? and roundId=?";
    @Override
    protected boolean updateData(CrossJiuZhouGuessData crossJiuZhouGuessData) {
        crossJiuZhouGuessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getGuessRoomIdMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getAllUserGuessNum())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getGuessResultMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getRoomWinnerMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGuessData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGuessData.getRoundId()));
        if (getDb().executeNoneQuery(updateCrossJiuZhouGuessDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossJiuZhouGuessDataSql = "insert into `t_c_jiu_zhou_round_guess_data`(activityId,roundId,guessRoomIdMap,allUserGuessNum,guessResultMap,roomWinnerMap) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossJiuZhouGuessData crossJiuZhouGuessData) {
        crossJiuZhouGuessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGuessData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGuessData.getRoundId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getGuessRoomIdMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getAllUserGuessNum())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getGuessResultMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGuessData.getRoomWinnerMap())));
        if (getDb().executeNoneQuery(insertCrossJiuZhouGuessDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossJiuZhouGuessData parseData(ResultSet rs) throws SQLException {

        int activityId = rs.getInt("activityId");
        int roundId = rs.getInt("roundId");

        CrossJiuZhouGuessData crossJiuZhouGuessData = new CrossJiuZhouGuessData(activityId, roundId);
        crossJiuZhouGuessData.setGuessRoomIdMap(JSON.parseObject(rs.getString("guessRoomIdMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        crossJiuZhouGuessData.setAllUserGuessNum(JSON.parseObject(rs.getString("allUserGuessNum"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.util.Map<java.lang.Long, java.lang.Integer>>>() {}));
        crossJiuZhouGuessData.setGuessResultMap(JSON.parseObject(rs.getString("guessResultMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.util.List<java.lang.Long>>>() {}));
        crossJiuZhouGuessData.setRoomWinnerMap(JSON.parseObject(rs.getString("roomWinnerMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Long>>() {}));

        return crossJiuZhouGuessData;
    }

}
